﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using OrderSystem;

namespace WindowsFormsOrder
{
    public partial class Form1 : Form
    {
        public OrderSystem.OrderService service = new OrderSystem.OrderService();
        public Form1()
        {
            service.import("D:\\code\\csharpprogram\\csHomework\\Week7\\OrderSystem\\OrderListTest.xml");
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.orderBindingSource.DataSource = service.orderlist;
            //this.dataGridView1.Columns[0].DataPropertyName = "num";
            //this.dataGridView1.Columns[1].DataPropertyName = "details";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                openFileDialog1.ShowDialog();
                openFileDialog1.CheckFileExists = true;
                openFileDialog1.AddExtension = true;
                openFileDialog1.Filter = "xml文件(*.xml)|";
                service.import(openFileDialog1.FileName);
                this.orderBindingSource.DataSource = service.orderlist;
            }
            catch (Exception)
            {

                textBox1.Text = "文件打开失败";
            }
            
        }

        private void button3_Click(object sender, EventArgs e)
        {
            //新增按钮
            Form2 newAddFrame = new Form2(this);
            newAddFrame.isADD = true;
            newAddFrame.Text = "新增";
            newAddFrame.ShowDialog();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            //修改按钮
            Form2 newAddFrame = new Form2(this);
            newAddFrame.isADD = false;
            newAddFrame.Text = "修改";
            newAddFrame.modifiedIndex = (int)dataGridView1.CurrentRow.Cells[0].Value;
            //newAddFrame.textBox1=
            newAddFrame.ShowDialog();
        }
        //在此使用字符串拆解实现通用主从对象绑定
        private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if ((dataGridView1.Rows[e.RowIndex].DataBoundItem != null) &&
    (dataGridView1.Columns[e.ColumnIndex].DataPropertyName.Contains(".")))
            {
                string[] nameAndProp = dataGridView1.Columns[e.ColumnIndex].DataPropertyName.Split(new char[] { '.' });
                object pObj = dataGridView1.Rows[e.RowIndex].DataBoundItem;
                for (int i = 0; i < nameAndProp.Length - 1; i++)
                {
                    pObj = GetObject(pObj, nameAndProp[i]);
                    if (pObj == null)
                    {
                        e.Value = string.Empty;
                        break;
                    }
                    if (i == nameAndProp.Length - 2)
                    {
                        //MemberInfo oM = pObj.GetType().GetMember(nameAndProp[i + 1])[0];
                        PropertyInfo objectProperty = pObj.GetType().GetProperty(nameAndProp[i + 1]);
                        e.Value = objectProperty.GetValue(pObj, null).ToString();
                        //e.Value = oM.Name;
                    }
                }
            }

        }
        //获取对象属性
        private object GetObject(object pObj, string nameAndProp)
        {
            if (pObj == null)
            {
                return null;
            }
            //MemberInfo o = pObj.GetType().GetMember(nameAndProp)[0];
            PropertyInfo objProp = pObj.GetType().GetProperty(nameAndProp);
            //o.Name
            return objProp.GetValue(pObj, null);
            //return (o.MemberType)pObj.GetType().GetProperty();
        }
        //搜索按钮事件，搜索必须指定搜索列名
        public void button6_Click(object sender, EventArgs e)
        {
            if (textBox1.Text != "") {
                string[] ng=textBox1.Text.Split(new char[] { '=' });
                try
                {
                    if (ng[0] == "good") { this.orderBindingSource.DataSource = new BindingList<OrderSystem.Order>(service.getOrder(new Good(ng[1])).ToList<Order>()); }
                    if (ng[0] == "customer") { this.orderBindingSource.DataSource = new BindingList<OrderSystem.Order>(service.getOrder(new Customer(ng[1])).ToList<Order>()); }
                    if (ng[0] == "num") { this.orderBindingSource.DataSource = new BindingList<OrderSystem.Order>(service.getOrder(int.Parse(ng[1])).ToList<Order>()); }
                    if (ng[0] == "amount") { this.orderBindingSource.DataSource = new BindingList<OrderSystem.Order>(service.getOrder(Double.Parse(ng[1])).ToList<Order>()); }
                }
                catch (Exception)
                {

                    this.orderBindingSource.DataSource = service.orderlist;
                    textBox1.Text = "未查询到此项";
                }
                
            }
            else
            {
                this.orderBindingSource.DataSource = new BindingList<OrderSystem.Order>(service.orderlist);
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            try
            {
                service.deleteOrder(service.getOrder((int)dataGridView1.CurrentRow.Cells[0].Value).First());
                this.orderBindingSource.DataSource = new BindingList<OrderSystem.Order>(service.orderlist);
            }
            catch (Exception)
            {
                
                textBox1.Text = "删除失败";
            }
            
            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                saveFileDialog1.InitialDirectory = "./";
                saveFileDialog1.Filter = "xml文件(*.xml)|";
                saveFileDialog1.CheckPathExists = true;
                saveFileDialog1.ShowDialog();
                service.export(saveFileDialog1.FileName);
            }
            catch (Exception)
            {

                textBox1.Text = "文件保存失败";
            }
            
        }
    }
}
